var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../../common/common');
var DBConfig = require('../../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);


// 获取商品详情
router.get('/jkyx/goods_detail', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	// 用户account_id
	var account_id = param.account_id || '';
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品id',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		var timer = CommonJS.getNowFormatDate();
		connection.query(DBConfig.getGoodsDetailByID,[goods_id,goods_id,goods_id,account_id,account_id,goods_id,account_id,goods_id,timer],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				// 商品
				var goods = results[0];
				// 处理评论
				var comments = results[1];
				var comments_length = comments.length;
				for (var i = 0; i < comments_length; i ++) {
					comments[i].create_time = CommonJS.formatDate(comments[i].create_time).split(' ')[0];
					comments[i].images = comments[i].images.split(',');
				}
				// 处理商品规格
				var specs = results[2];
				var specs_len = specs.length;
				var salecount = 0;
				for (var i = 0; i < specs_len; i ++) {
					salecount = salecount + specs[i].salecount; 
				}
				var detail = {
					id: goods[0].id,
					name: goods[0].name,
					descr: goods[0].descr,
					imgs: JSON.parse(goods[0].imgs),
					detail_imgs: goods[0].detail_imgs,
					spec: specs,
					salecount: salecount
				}
				// 购物车数量
				var cartNum = 0;
				if (account_id != '') {
					cartNum = results[3][0]['count(*)'];
				}
				// 是否收藏
				var collection = '2';
				if (results[4].length > 0) {
					collection = results[4][0].status;
				} 
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			detail: detail,
	     			comments: comments,
	     			cartNum: cartNum,
	     			collection: collection
	     		}));	  
			}
		})
	})
});


// 商品推荐
router.get('/jkyx/goods_tuijian', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品id',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		var sql = 'select classify_id from goods_classify_relation where goods_id = ?';
		connection.query(sql,[goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var classify_id = results[0].classify_id;
				var reqUrl = 'http://localhost:8100/jkyx/goods_list?classify_id=' + classify_id;
				request(reqUrl, function (error, response, body) {
		   			if (!error && response.statusCode == 200) {
		   				//原数组
						var arr = JSON.parse(body).goods;
						//输出数组
						var out = [];
						//输出个数
						var num = 6;
						if (arr.length <= 6) {
							num = 3;
						}
						while(out.length < num){
						    var temp = (Math.random()*arr.length) >> 0;
						    var tempItem = arr.splice(temp,1)[0];
						    if (tempItem.id != goods_id) { // 去除相同的
						    		out.push(tempItem);
						    } 
						}
		   				res.end(JSON.stringify({
			     			msg: '操作成功',
			     			status:'100',
			     			goods: out
			     		}));	  
		   			}else{
		   				res.end(JSON.stringify({
			     			msg: '请求失败',
			     			status:'101',
			     			goods: []
			     		}));	 
		   			}
		   		})
			}
		})
	})
});




module.exports = router;
